package com.huazi.www.yang.login.dao.Impl;

import com.huazi.www.yang.login.dao.DocdorLoginDao;
import com.huazi.www.yang.login.entity.DoctorLogin;
import com.huazi.www.yang.login.uilt.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;

public class DocdorLoginDaoImpl extends BaseDao implements DocdorLoginDao {
    ResultSet rs = null;

    /**
     * 医生登录
     *
     * @param docdorId 医生id
     * @param pwd      密码
     * @return
     */
    @Override
    public int login(String docdorId, String pwd) {
        String sql = "SELECT count(1) FROM `doctor_login` where doctor_login.doctorid=? AND doctor_login.doctorpwd=?";
        rs = executeselect(sql, docdorId, pwd);
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 忘记密码
     *
     * @param doctorId 医生用户id
     * @return
     */
    @Override
    public int forgetPwd(String doctorId, String doctorcard) {
        String sql = "select count(1) from doctor_information where doctor_information.doctorId=? and doctor_information.doctorcard=?";
        rs = executeselect(sql, doctorId, doctorcard);
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 修改密码
     *
     * @param doctorId
     * @param pwd
     * @return
     */
    @Override
    public int updatePwd(String doctorId, String pwd) {
        String sql = "UPDATE doctor_login set doctor_login.doctorpwd=? where doctor_login.doctorid=?";
        int count = executeupdate(sql, pwd, doctorId);
        return count;
    }

    @Override
    public DoctorLogin doctor(String id) {
        //创建医生对象
        DoctorLogin doctorLogin = null;
        String sql = "select doctorId,doctorname,position from doctor_information where doctorId = ?";
        rs = executeselect(sql, id);
        try {
            rs.next();
            String doctorID = String.valueOf(rs.getInt(1));
            String doctorName = rs.getString(2);
            String position = rs.getString(3);
            doctorLogin = new DoctorLogin(doctorID, doctorName, position);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return doctorLogin;
    }

}
